package com.suyuan.controller.shop.admin;

import com.suyuan.entity.HotProduct;
import com.suyuan.entity.ProPic;
import com.suyuan.entity.ProductInfo;
import com.suyuan.service.ProPicService;
import com.suyuan.service.ProductService;
import com.suyuan.vo.HotProductVO;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 热门商品
 */
@Controller
@RequestMapping(value = "/shop/admin/hotProduct")
public class HotProduct2Controller {
    @Resource(name = "productService")
    private ProductService productService;
    @Resource(name  = "proPicService")
    private ProPicService proPicService;

    @RequestMapping(value = "/list")
    public String list(ModelMap model){
        List<HotProductVO> hotProducts = productService.getHotProduct();
        model.addAttribute("hotProducts",hotProducts);
        model.addAttribute("size",hotProducts.size());
        return "hot_product_list";
    }

    @RequestMapping(value = "/add")
    public String add(ModelMap model){
        List<ProductInfo> all = productService.findAll();
        model.addAttribute("products",all);
        return "/hot_product_add";
    }

    @RequestMapping(value = "/save")
    public String save(HttpServletRequest request,Integer product1, Integer product2, Integer product3){
        String realPath = request.getServletContext().getRealPath("/WEB-INF/images/");

        if(product1 == null || product2 == null || product3 == null){
            System.out.println("商品信息不能为空");
            return "redirect:/shop/admin/hotProduct/list";
        }

        try {
            if (request instanceof MultipartHttpServletRequest) {
                System.out.println("222");
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                // 获取上传的文件集合
                Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
                for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
                    MultipartFile mutiFile = entry.getValue();
                    // 对文件进处理
                    String uuid = UUID.randomUUID().toString().replace("-","");
                    String extension = FilenameUtils.getExtension(mutiFile.getOriginalFilename());
                    String newFileName = uuid + "." + extension;

                    ProPic pic = new ProPic();
                    pic.setProductId(product1);
                    pic.setUrl("/images/"+newFileName);
                    pic.setType(3);//热门商品大图类型
                    proPicService.insert(pic);

                    HotProduct hot = new HotProduct();
                    hot.setPro1(product2);
                    hot.setPro2(product3);
                    hot.setPropicId(pic.getId());
                    productService.insertHotProduct(hot);
                    mutiFile.transferTo(new File(realPath+"/"+newFileName));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/shop/admin/hotProduct/list";
    }

    @RequestMapping(value = "/delete")
    public String delete(Integer id){
        productService.deleteHotProduct(id);
        return "redirect:/shop/admin/hotProduct/list";
    }


}
